<?php  
	
	  include "../include/session_config.php";
	  include "../include/config.php";
	  include "../include/function.php";
	  include "../include/class_userlogin.php";
	  #### segment ###
	  include "THSplitLib/THSplitLib/segment.php";
	  
	  $list=new UserLogin();
	  $segment = new Segment();
		db_connect();
	  $path="../uploads/file/";
	

	
	
	
// set notifybar
 $_SESSION["show"]="show";
 if(@$_GET['act']=="delete"){
 	$_SESSION["shw_type"]="delete";
 }
 if($_POST['id']!=""){
 	$_SESSION["shw_type"]="edit";
 }else{
 	$_SESSION["shw_type"]="add";
 }
 	
//var_dump($_POST,$_FILES);exit;

function get_detail($id,$method=false)
{
		$name1['problemname']=GetOne("problemname","id",$_POST['problemtype'],"problemtype");
		$name2['systemname']=GetOne("systemname","id",$_POST['systemid'],"system");

		if($id!=""){			
			$field="code,problemtype,title,status,chk_send
			,send_note
			,active_date,send_date,new_date,operation_date,complete_date
			,systemid,orderid,responsibleid,coordinatorid,ownid
			,service,system_success,response_success";
			$result=mysql_query("select $field from request_lists where id='$id'");
			$item=mysql_fetch_assoc($result);
			
			$str=array("code"=>$_POST['code'],"problemtype"=>$_POST['problemtype'],"title"=>$_POST['title'],"status"=>$_POST['status'],"chk_send"=>@$_POST['chk_send']
					   ,"send_note"=>$_POST['send_note']
					   ,"active_date"=>$_POST['active_date'],"send_date"=>$_POST['send_date'],"new_date"=>$_POST['new_date']
					   ,"operation_date"=>$_POST['operation_date'],"complete_date"=>$_POST['complete_date']
					   ,"systemid"=>$_POST['systemid'],"orderid"=>$_POST['orderid'],"responsibleid"=>$_POST['responsibleid'],"coordinatorid"=>$_POST['coordinatorid']
					   ,"ownid"=>$_POST['ownid'],"service"=>@$_POST['service'],"system_success"=>$_POST['system_success'],"response_success"=>@$_POST['response_success']
						);		
			
	
			$arr_key=array_diff($str,$item);		 
			if(empty($arr_key)){
					$arr_key =array_diff($item,$str);
			}
			
			if(empty($arr_key)){
				$conj="รหัส : ".$_POST['code'];
				$detail=" ไม่มีการเปลี่ยนแปลงใดๆ";
				$save_detail=$conj.$detail;
			}else{
				if($method=="post"){
					foreach($arr_key as $key=>$value){
						$detail[]="รหัส : ".$_POST['code'];
						$detail[]="หัวข้อ :" .$_POST['title'];
						$detail[]="ประเภทปัญหา :".$name1['problemname'];
						$detail[]="ระบบ :".$name2['systemname'];					
					}// foreach
				}else{
					$row=GetData("request_lists",$id);
					$name1['problemname']=GetOne("problemname","id",$row['problemtype'],"problemtype");
					$name2['systemname']=GetOne("systemname","id",$row['systemid'],"system");
						$detail[]="รหัส : ".$row['code'];
					$detail[]="หัวข้อ :" .$row['title'];
					$detail[]="ประเภทปัญหา :".$name1['problemname'];
					$detail[]="ระบบ :".$name2['systemname'];
				}//close method
			$detail=implode(", ",$detail);
			$save_detail="ดังนี้ : ".$detail;
			
			}//close if $arr_key
		
		
		}else{
			$detail[0]="รหัส : ".$_POST['code'];
			$detail[1]="หัวข้อ :" .$_POST['title'];
			$detail[2]="ประเภทปัญหา :".$name1['problemname'];
			$detail[3]="ระบบ :".$name2['systemname']	;
			
			$detail=implode(", ",$detail);
			$save_detail="ดังนี้ : ".$detail;
			
		}//close if $id
		

				
	return $save_detail; 
}//close function
	





if(@$_GET['act']=='delete'){
   
	//$detail=get_detail();
	$detail=get_detail($_GET['id'],"get");
	mysql_query("DELETE FROM request_lists WHERE id=".$_GET['id']);
	mysql_query("DELETE FROM request_list_details WHERE title_id=".$_GET['id']);
	mysql_query("DELETE FROM request_list_note WHERE request_lists_id=".$_GET['id']) or die("del request_list_note :".mysql_error());
	$item=GetData("request_list_details",$_GET['id']);
	if($item)
	{
		
		unlink ($path.@$item['fileatth']);
	}
	$list->AddLog(41,$detail);
	ReDirect($host."request_list.php",'top');
	
}else{
	
//เช็ค status กรณีติีกเสร็จทั้งสองฝ่าย
if(@$_POST['chk_send']=="send_wait"){
	if(@$_POST['system_success']=="1" && @$_POST['response_success']=="1"){
	$_POST['status']="3";
	//เสร็จเรียบร้อยแล้ว
	}else if(@$_POST['system_success']!="1" || $_POST['response_success']!="1"){
		$_POST['status']="4";
		//ส่งต่อการดำเนินการ
	}
}else if(@$_POST['chk_send']=="send"){
	if($_POST['system_success']=="1"){
		$_POST['status']="3";
	}else{
		$_POST['status']="4";
	}
}	
	
switch(@$_POST['status'])
{
	case "1":
		$_POST['new_date']=($_POST['new_date']=="0000-00-00 00:00:00" || $_POST['new_date']=="" ||$_POST['new_date']==NULL)?date('Y-m-d H:i:s'):$_POST['new_date'];	
		break;
	case "2":
		$_POST['operation_date']=($_POST['operation_date']=="0000-00-00 00:00:00" || $_POST['operation_date']=="" ||$_POST['operation_date']==NULL)?date('Y-m-d H:i:s'):$_POST['operation_date'];
		break;
	case "3":
		$_POST['complete_date']=($_POST['complete_date']=="0000-00-00 00:00:00" || $_POST['complete_date']=="" ||$_POST['complete_date']==NULL)?date('Y-m-d H:i:s'):$_POST['complete_date'];
		break;

}

//
if((@$_POST['chk_send']=='send' || @$_POST['chk_send']=='send_wait') && @$_POST['send_date']=="0000-00-00 00:00:00"){
	$_POST['send_date']=date('Y-m-d H:i:s');
}


if(($_POST['active_date']=="0000-00-00 00:00:00" || $_POST['active_date']=="") && $_SESSION['usertype']==1){
	$_POST['active_date']=date('Y-m-d H:i:s');
}

// code --> part#1
$result=mysql_query("select * from problemtype where id='".@$_POST['problemtype']."'");
$item=mysql_fetch_assoc($result);
$abbr=$item['abbr'];
$part_code=$item['abbr'].date('Ym');
//code --> part#2
$sql="select  * from request_lists  where  substr(code,1,7)='".$part_code."'";
$result=mysql_query($sql);
$n_row = mysql_num_rows($result);
$n_row=$n_row+1;

//code -->part#3  
switch (strlen($n_row)){
	case "1":
		$rec_no="00".$n_row; break;
	case "2":
		$rec_no="0".$n_row;  break;
	default:
		$rec_no=$n_row;      break;
}
//code  -->aggreation
	
	if($_POST['code']!=""){
		$_POST['code']=$abbr.trim(substr($_POST['code'],1));
		//print $_POST['code'];exit;
	
	}else{
		$_POST['code']=$abbr.date('Ym').$rec_no;
	}
	

		
			


if(@$_POST['id']==""){
				$field  ="problemtype,title,status,chk_send";
				$field .=",send_note";
				$field .=",new_date,operation_date,complete_date";
				$field .=",orderid,responsibleid,coordinatorid,systemid";
				$field .=",service,code,send_date,active_date,system_success,response_success,ownid";
				
				
	switch($_SESSION['usertype'])
		{
			case "1": 	
			//ผู้รับผิดชอบ	
				$val  ="'".$_POST['problemtype']."','".$_POST['title']."','".$_POST['status']."','".@$_POST['chk_send']."'";						
				$val .=",'".$_POST['send_note']."'";
				$val .=",'".$_POST['new_date']."','".$_POST['operation_date']."','".$_POST['complete_date']."'";
				$val .=",'".$_POST['orderid']."','".$_POST['responsibleid']."','".$_POST['coordinatorid']."','".$_POST['systemid']."','".@$_POST['service']."'";
				$val .=",'".$_POST['code']."','".@$_POST['send_date']."','".@$_POST['active_date']."'";
				$val .=",'".@$_POST['system_success']."','".@$_POST['response_success']."',''";
				break;
			
			case "2": 	
			//ผู้ประสานงาน
				$val="'".$_POST['problemtype']."','".$_POST['title']."','".$_POST['status']."',''";						
				$val .=",''";
				$val .=",'".$_POST['new_date']."','',''";
				$val .=",'".$_POST['orderid']."','','".$_POST['coordinatorid']."','".$_POST['systemid']."','".$_POST['service']."'";
				$val .=",'".$_POST['code']."','','','','',''";
				break;
			case "4":	
			//ผู้ใช้งาน
				$val="'".$_POST['problemtype']."','".$_POST['title']."','".$_POST['status']."',''";						
				$val  .=",''";
				$val  .=",'".$_POST['new_date']."','',''";
				$val  .=",'".$_POST['orderid']."','','','".$_POST['systemid']."','".$_POST['service']."'";
				$val .=",'".$_POST['code']."','','','','',''";
				break;
			case "3":  
			//เจ้าของระบบ
				$val="'".$_POST['problemtype']."','".$_POST['title']."','".$_POST['status']."',''";						
				$val .=",''";
				$val .=",'".$_POST['new_date']."','',''";
				$val .=",'".$_POST['orderid']."','','','".$_POST['systemid']."','".$_POST['service']."'";
				$val .=",'".$_POST['code']."','',''";
				$val .=",'".@$_POST['system_success']."','".@$_POST['response_success']."','".$_POST['ownid']."'";
				break;
		}
	
	       //var_dump($_POST['k_before'],$_FILES);exit;
			mysql_query("LOCK TABLES request_lists WRITE"); 
			mysql_query("SET AUTOCOMMIT = 0");	
	
			mysql_query("INSERT INTO request_lists(".$field.") VALUES(".$val.")") or die("Error insert :".mysql_error()); 
			$title_id = mysql_insert_id(); 
			mysql_query("COMMIT");
			mysql_query("UNLOCK TABLES");	
			

			
			
			
	for($i=1;$i<=$_POST['k_before'];$i++){

		$sql="INSERT INTO request_list_details(detail,title_id) VALUE('".$_POST['detail'.$i]."','".$title_id."')" ;				
		mysql_query($sql) or die("Error 1: ".mysql_error());	
		$insert_id = mysql_insert_id(); 
		
		$sql="UPDATE request_list_details SET url='".$_POST['url'.$i]."',title_id=".$title_id." WHERE id=".$insert_id;				
		mysql_query($sql) or die("Error 2 : ".mysql_error());
	

		
	   
	   	
		// upload file
		 if($_FILES['fileatth'.$i]!=""){
				 
			$sur = strrchr(@$_FILES["fileatth".$i]["name"], "."); //ตัดนามสกุลไฟล์เก็บไว้
			$arr = (Date("dmy_His").$sur); //ผมตั้งเป็น วันที่_เวลา.นามสกุล			
			$sql="UPDATE request_list_details SET fileatth='".$arr."',title_id=".$title_id." WHERE id=".$insert_id;				
			
			  if (@$_FILES["fileatth".$i]["error"] > 0)
				{
				$error_message = $error_types[$_FILES['fileatth']['error'][$i]];
				//echo "size: " .$_FILES["fileatth"]['size'][$i];
				//echo "Error: " .@$_FILES["fileatth"]["error"][$i] ." :".$error_message. "<br>";
				//exit;
				}
			  else
				{		$path="../uploads/file/";				
						if (file_exists($path.@$_FILES["fileatth".$i]["name"])) 
						{
							//echo "file exists";
							unlink ($path.@$_FILES["fileatth".$i]["name"]);
						}else{
							//$sur = strrchr(@$_FILES["fileatth".$i]["name"], "."); //ตัดนามสกุลไฟล์เก็บไว้
							//$filename = (Date("dmy_His").$sur); //ผมตั้งเป็น วันที่_เวลา.นามสกุล		
							//var_dump($path,$arr);exit;
							move_uploaded_file($_FILES["fileatth".$i]["tmp_name"],$path.@$arr);							
						}
				}
				mysql_query($sql) or die("Error 3: ".mysql_error());
			}//close if isset

	}//close for $_POST['k_before']
	
			$detail=get_detail("");
			$list->AddLog(39,$detail);


}
else{
	################# UPDATE  #####################
	//เจ้าของระบบ ส่งต่อกรณีงานเดี่ยว
	
	
	$detail=get_detail($_POST['id'],"post");
	
	$val  ="id='".$_POST['id']."'";
	$val .=",problemtype='".$_POST['problemtype']."'";
	$val .=",status='".$_POST['status']."'";
	$val .=",chk_send='".@$_POST['chk_send']."'";
	$val .=",send_note='".@$_POST['send_note']."'";
	$val .=",send_date='".$_POST['send_date']."'";
	$val .=",new_date='".$_POST['new_date']."'";
	$val .=",operation_date='".@$_POST['operation_date']."'";
	$val .=",complete_date='".@$_POST['complete_date']."'";
	$val .=",orderid='".$_POST['orderid']."'";
	$val .=",responsibleid='".@$_POST['responsibleid']."'";
	$val .=",coordinatorid='".@$_POST['coordinatorid']."'";
	$val .=",systemid='".@$_POST['systemid']."'";
	$val .=",service='".@$_POST['service']."'";
	$val .=",code='".@$_POST['code']."'";
	$val .=",active_date='".@$_POST['active_date']."'";
	$val .=",system_success='".@$_POST['system_success']."'";
	$val .=",response_success='".@$_POST['response_success']."'";
	$val .=",ownid='".@$_POST['ownid']."'";
	$sql="UPDATE request_lists SET ".$val." WHERE id=".$_POST['id'];	
	//print($sql);exit;
	mysql_query($sql)or die("Error update :".mysql_error());
	
    $txt_email="(update)";
	

   
   
   
	mysql_query("delete from request_list_details where title_id='".$_POST['id']."'") or die("Error delete :".mysql_error());
	for($i=1;$i<=$_POST['k_before'];$i++){

		$sql="INSERT INTO request_list_details(detail,title_id) VALUE('".$_POST['detail'.$i]."','".$_POST['id']."')" ;				
		mysql_query($sql) or die("Error 1: ".mysql_error());	
		$insert_id = mysql_insert_id(); 
		
		$sql="UPDATE request_list_details SET url='".$_POST['url'.$i]."',title_id=".$_POST['id']." WHERE id=".$insert_id;				
		mysql_query($sql) or die("Error 2 : ".mysql_error());
	    
	  $path="../uploads/file/";		
	   if($_FILES['fileatth'.$i]['name']==""){
		   
		   $arr=@$_POST['fileatth'.$i];
	   }else{
	   	   //$arr=$_FILES['fileatth'.$i]['name'];
		   $sur = strrchr(@$_FILES["fileatth".$i]["name"], "."); //ตัดนามสกุลไฟล์เก็บไว้
		   $arr = (Date("dmy_His").$sur); //ตั้งเป็น วันที่_เวลา.นามสกุล		
	   }
		$sql="UPDATE request_list_details SET fileatth='".$arr."',title_id=".$_POST['id']." WHERE id=".$insert_id;				
	    // upload file
		
		 if($_FILES['fileatth'.$i]!=""){
			 
			  if (@$_FILES["fileatth".$i]["error"] > 0)
				{
				$error_message = $_FILES['fileatth'.$i]['error'][$i];
				//echo "size: " .$_FILES["fileatth"]['size'][$i];
				echo "Error: " .@$_FILES["fileatth"]["error"][$i] ." :".$error_message. "<br>";
				//exit;
				}
			  else
				{						
						if (file_exists($path.@$_FILES["fileatth".$i]["name"])) 
						{
							//echo "file exists";
							unlink ($path.@$_FILES["fileatth".$i]["name"]);
						}else{
							//$sur = strrchr(@$_FILES["fileatth".$i]["name"], "."); //ตัดนามสกุลไฟล์เก็บไว้
							//$filename = (Date("dmy_His").$sur); //ผมตั้งเป็น วันที่_เวลา.นามสกุล									
							//echo $path.@$arr;exit;
							move_uploaded_file(@$_FILES["fileatth".$i]["tmp_name"],$path.@$arr);							
						}
				}
			}//close if isset
		mysql_query($sql) or die("Error 3: ".mysql_error());
		//exit;				
		//<<<<<<<<<<<<<<<<<<<<<<<<    ตัำดตำ           >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
			
          if($_POST['detail'.$i]!="")
		   {
	            $result_segment=array();
	            $concat=$_POST['title']."  ".$_POST['detail'.$i];				   				  
	            $concat = preg_replace('|[\!\@\#\$\%\^\&\*\(\)\-\=\_\+\[\]\{\}\;\'\:\"\,\.\<\>\/\?\\\\\|\~\�\�\n\r ]|', '  ', $concat);
	            $result_segment = $segment->get_segment_array($concat,$arr);	
				$count_keyword=$segment->_segment_count_keyword($result_segment);				
				//echo $concat;
				echo '<pre>';
				print_r($result_segment);
				print_r($count_keyword);
				echo '</pre>';								
	            //echo implode(' | ', $result_segment);
	            echo '<hr/>';
	            /*echo '<b>คำที่อาจจะตัดผิด:</b><br/>';
			    foreach($result_segment as $row)
	            {
	                if (mb_strlen($row) > 12)
	                {
	                    echo $row.'<br/>';
	                }
	            }*/
				//exit;
				echo '<table class="tblist">';
				echo '<tr>';
				echo '<th></th>';
				echo '</tr>';
				echo '</table>';
			
		 }
		//<<<<<<<<<<<<<<<<<<<<<<<<    ตัำดตำ           >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>		
	
	}//for

	
	
}
	


	
	

 } // close act

	

?>
